# Exponential Moving Average (EMA)
import pandas as pd
import statistics as stats

'''
   @ param in
   DataSet: dataset object list 
   NumPeriods: ema cal periods
 
   @ return
   DataSet with ExponentialMovingAverage data
'''
def cal_ema(DataSet, NumPeriods):
	data_set_ = pd.DataFrame(DataSet)
    close = data_set_['close'].astype(float)
	num_periods = TimePeriod # number of days over which to average
	K = 2 / (num_periods + 1)
	ema_p = 0
	ema_values = []
	for close_price in close:
		if (ema_p == 0):
			ema_p = close_price
		else:
			ema_p = (close_price - ema_p) * K + ema_p
		ema_values.append(ema_p)
	data_set_ = data_set_.assign(ExponentialMovingAverage=pd.Series(sma_values, index=data_set_.index))
	return data_set_
